/*document on ready*/
$(function() {
	
});

/*客製化Block UI*/
function blockui() {
	$.blockUI();
}

function unBlockui() {
	$.unblockUI();
}

/**/
$('form').keypress(function(event){

	if (event.keyCode == 10 || event.keyCode == 13) 
	    event.preventDefault();

});

/*確認session上有沒有登入使用者*/
function checkSessionUser() {
	
	var result = false;
	$.ajax({
		  url: 'home_checkSessionUser.action',
		  context: this,
		  type: 'POST',
		  dataType:"json",
		  async: false, 
		  success: function(json) {
			  if (json.length > 0) {
				  if (json.trim() == "true") {
					  result = true;
				  } else {
					  result = false;
				  }
			  }
		  },
		  error: function (xhr, ajaxOptions, thrownError) {
			  result = false;
		  }
		});
	
	return result;
}

/*登入使用者*/
function loginUser() {
	
	var email = $("input[name*='loginEmail']").val();
	var password = $("input[name*='loginPassword']").val();
	
	if (email == null 
			|| password == null){
		return;
	}
	
	if (email.trim() == ''
			|| password.trim() == '') {
		return ;
	}
	
	$.ajax({
		  url: 'home_loginUser.action',
		  context: this,
		  type: 'POST',
		  dataType:"json",
		  async: false, 
		  data: $('form').serialize(),
		  success: function(json) {
			  if (json != null) {
				  if (json.length > 0) {
					  if (json.trim() == "true") {
						  $('#loginModal').modal('hide');
					  } else {
						  $('#loginModal').modal('show');
						  $('#messageBar').empty();
						  $('#messageBar').append('<p class="text-error">請輸入正確Email與Password!!</p>');
					  }
				  }
			  }			  
		  }
		});
}

/*登出使用者*/
function logoutUser() {
	
	$.ajax({
		  url: 'home_logoutUser.action',
		  context: this,
		  type: 'POST',
		  dataType:"json",
		  async: false, 
		  data: $('form').serialize(),
		  success: function(json) {
			  if (json != null) {
				  if (json.length > 0) {
					  if (json.trim() == "true") {
						  $('#logoutModal').modal('show');
						  $("form").attr({'action':'home_welcome',target:'_self'});
						  $("form").submit();
					  } else {
						  $("form").attr({'action':'home_welcome',target:'_self'});
						  $("form").submit();
					  }
				  }
			  }			  
		  }
		});
}

/*ajax add user*/
function ajaxAddUser(){
	
	var email = $("input[name*='userMemberBo.Email']").val();
	var password = $("input[name*='userMemberBo.Password']").val();
	var comfrimPassword = $("input[name*='userMemberBo.ComfrimPassword']").val();
	var name = $("input[name*='userMemberBo.Name']").val();
	var handphone = $("input[name*='userMemberBo.Handphone']").val();
	var address = $("input[name*='userMemberBo.Address']").val();
	
	var alertMsg = "";
	
	if (email == null || email=="") {
		alertMsg += "請輸入Email Address 電子信箱!\n";
	}
	
	if (password == null || password=="") {
		alertMsg += "請輸入Password 密碼!\n";
	}
	
	if (comfrimPassword == null || comfrimPassword=="") {
		alertMsg += "請輸入Confrim Password 密碼確認!\n";
	}
	
	if (name == null || name=="") {
		alertMsg += "請輸入Member Name 會員姓名!\n";
	}
	
	if (handphone == null || handphone=="") {
		alertMsg += "請輸入Mobile 手機號碼!\n";
	}
	
	if (address == null || address=="") {
		alertMsg += "請輸入Address 聯絡地址!\n";
	}
	
	if (password != comfrimPassword) {
		alertMsg += "Password 密碼與Confrim Password 密碼確認輸入不同!\n";
	}
	
	if (alertMsg.trim() != ""){
		alert(alertMsg);
		return;
	}
	
	$.ajax({
		  url: 'home_addUser.action',
		  context: this,
		  type: 'POST',
		  dataType:"json",
		  async: false, 
		  data: $('form').serialize(),
		  success: function(json) {
			  if (json != null) {
				  if (json.length > 0) {
					  if (json.trim() == "true") {
						  $('#loginModal').modal('hide');
					  } else {
						  $('#loginModal').modal('show');
						  $('#messageBar').empty();
						  $('#messageBar').append('<p class="text-error">請輸入正確會員資料或許該Email已被註冊!!</p>');
					  }
				  }
			  }			  
		  }
		});
}

/*close window*/
function closeWindow(){
	window.close();
}

/*ajax explain12*/
function ajaxExplain12() {
	
	//check session user
	if (checkSessionUser() == false) {
		$('#loginModal').modal('toggle');
		return;
	}
	
	//redirect page
	$("form").attr({'action':'explain_explain12',target:'_self'});
	$("form").submit();
}

/*ajax explain1201*/
function ajaxExplain1201() {
	
	//check session user
	if (checkSessionUser() == false) {
		$('#loginModal').modal('toggle');
		return;
	}
	
	//blockui();
	$.ajax({
		  url: 'explain_explain1201.action',
		  context: this,
		  type: 'POST',
		  dataType:"json",
		  async: false, 
		  data: $('form').serialize(),
		  success: function(json) {
			  if (json != null) {
				  if (json.length > 0) {
					  if (json.trim() == "true") {
						  $('#sendMessageBar').empty();
						  $('#sendMessageBar').append('<p class="font-warring-02">已成功發出Email通知Honeydew囉!!</p>');
					  } else {
						  $('#sendMessageBar').empty();
						  $('#sendMessageBar').append('<p class="font-warring-02">不好意思，發生問題請電洽Honeydew!!</p>');
					  }
				  }
			  }			  
		  }
		});
	
	
}

/*ajax userInfor01*/
function ajaxUserInfor01() {
	//check session user
	if (checkSessionUser() == false) {
		$('#loginModal').modal('toggle');
		return;
	}
	
	blockui();
	//redirect page
	$("form").attr({'action':'userInfor_userInfor01',target:'_self'});
	$("form").submit();
	
}

/*ajax userInfor0101*/
function ajaxUserInfor0101() {
	//check session user
	if (checkSessionUser() == false) {
		$('#loginModal').modal('toggle');
		return;
	}
	
	blockui();
	$.ajax({
		  url: 'userInfor_userInfor0101.action',
		  context: this,
		  type: 'POST',
		  dataType:"json",
		  async: false, 
		  data: $('form').serialize(),
		  success: function(json) {
			  if (json != null) {
				  if (json.length > 0) {
					  if (json.trim() == "true") {
						  $("form").attr({'action':'userInfor_userInfor01',target:'_self'});
						  $("form").submit();
					  } else {
						  $("form").attr({'action':'home_welcome',target:'_self'});
						  $("form").submit();
					  }
				  }
			  }			  
		  }
		});
}

/*ajax userInfor0102*/
function ajaxUserInfor0102() {
	
	var oldPassword = $("#oldPassword").val();
	var newPassword = $("#newPassword").val();
	var comfrimPassword = $("#comfrimPassword").val();
	
	var alertMsg = "";
	
	if (oldPassword == null || 
			oldPassword.trim() == "") {
		alertMsg += "請輸入舊密碼 !\n";
	}
	
	if (newPassword == null || 
			newPassword.trim() == "") {
		alertMsg += "請輸入新密碼 !\n";
	}
	
	if (comfrimPassword == null || 
			comfrimPassword.trim() == "") {
		alertMsg += "請輸入再確認新密碼 !\n";
	}
	
	if (newPassword.trim() != 
		comfrimPassword.trim()) {
		alertMsg += "請確認新密碼與確認新密碼是否輸入正確 !\n";
	}
	
	if (alertMsg.trim() != "") {
		alert(alertMsg);
		return;
	}
	
	//check session user
	if (checkSessionUser() == false) {
		$('#loginModal').modal('toggle');
		return;
	}
	
	blockui();
	$.ajax({
		  url: 'userInfor_userInfor0102.action',
		  context: this,
		  type: 'POST',
		  dataType:"json",
		  async: false, 
		  data: $('form').serialize(),
		  success: function(json) {
			  if (json != null) {
				  if (json.length > 0) {
					  if (json.trim() == "true") {
						  $("form").attr({'action':'userInfor_userInfor01',target:'_self'});
						  $("form").submit();
					  } else {
						  $("form").attr({'action':'home_welcome',target:'_self'});
						  $("form").submit();
					  }
				  }
			  }			  
		  }
		});
}

/*ajax userInfor02*/
function ajaxUserInfor02() {
	//check session user
	if (checkSessionUser() == false) {
		$('#loginModal').modal('toggle');
		return;
	}
	
	blockui();
	//redirect page
	$("form").attr({'action':'userInfor_userInfor02',target:'_self'});
	$("form").submit();
}

/*ajax userInfor0201*/
function ajaxUserInfor0201(param) {
	//check session user
	if (checkSessionUser() == false) {
		$('#loginModal').modal('toggle');
		return;
	}
	
	blockui();
	$.ajax({
		  url: 'userInfor_userInfor0201.action',
		  context: this,
		  type: 'POST',
		  async: false, 
		  data: {orderInforSeqid : param},
		  success: function(response) {
			  if (response != null) {
				  if (response.length > 0) {
					  var x=window.open('', '_blank', 'resizable=yes');
					  x.document.open();
					  x.focus();
					  x.document.write(response);
				  }
			  }			  
		  }
		});
	
}

/*ajax userInfor03*/
function ajaxUserInfor03() {
	//check session user
	if (checkSessionUser() == false) {
		$('#loginModal').modal('toggle');
		return;
	}
	
	blockui();
	//redirect page
	$("form").attr({'action':'userInfor_userInfor03',target:'_self'});
	$("form").submit();
}

/*ajax userInfor04*/
function ajaxUserInfor04() {
	//check session user
	if (checkSessionUser() == false) {
		$('#loginModal').modal('toggle');
		return;
	}
	
	
}

/*ajax login*/
function ajaxLogin01(){
	
	//check session user
	if (checkSessionUser() == false) {
		$('#loginModal').modal('toggle');
		return;
	}
	
}

/*ajax orderList01*/
function ajaxOrderList01() {
	//check session user
	if (checkSessionUser() == false) {
		$('#loginModal').modal('toggle');
		return;
	}
	
	blockui();
	//redirect page
	$("form").attr({'action':'orderList_orderList01',target:'_self'});
	$("form").submit();
}

/*ajax orderList0101*/
function ajaxOrderList0101(param) {
	//check session user
	if (checkSessionUser() == false) {
		$('#loginModal').modal('toggle');
		return;
	}
	
	$.ajax({
		  url: 'orderList_orderList0101.action',
		  context: this,
		  type: 'POST',
		  async: false, 
		  data: {orderInforSeqid : param},
		  success: function(response) {
			  if (response != null) {
				  if (response.length > 0) {
					  var x=window.open('', '_blank', 'resizable=yes');
					  x.document.open();
					  x.focus();
					  x.document.write(response);
				  }
			  }			  
		  }
		});
	
}

/*檢查是否還有庫存*/
function checkItemStock() {
	
	var result = false;
	
	blockui();
	$.ajax({
		  url: 'index_checkItemStock.action',
		  context: this,
		  type: 'POST',
		  dataType:"json",
		  async: false, 
		  data: $('form').serialize(),
		  success: function(json) {
			  if (json.length > 0) {
				  if (json.trim() == "true") {
					  result = true;
				  } else {
					  result = false;
				  }
			  }
		  },
		  error: function (xhr, ajaxOptions, thrownError) {
			  result = false;
		  }
		});
	
	return result;
}

/*ajax index add shopping car*/
function ajaxAddShoppingCar() {
	
	blockui();
	//check item stock
	if (checkItemStock() == false) {
		alert("抱歉現在庫存不足請見諒!!");
		$("form").attr({'action':'home_welcome',target:'_self'});
		$("form").submit();
		return;
	}
	
	$.ajax({
		  url: 'index_addShoppingCar.action',
		  context: this,
		  type: 'POST',
		  dataType:"json",
		  async: false, 
		  data: $('form').serialize(),
		  success: function(json) {
			  if (json != null) {
				  if (json.length > 0) {
					  if (json.trim() == "true") {
						  $("form").attr({'action':'index_indexQuery',target:'_self'});
						  $("form").submit();
					  } else {
						  $("form").attr({'action':'home_welcome',target:'_self'});
						  $("form").submit();
					  }
				  }
			  }			  
		  },
		  error: function (xhr, ajaxOptions, thrownError) {
			  alert(xhr);
		  }
		});
}

/*ajax index query paging*/
function ajaxIndexQueryPaging(param) {
	
	$("#pageNum").val(param);
	
	blockui();
	$("form").attr({'action':'index_indexQuery',target:'_self'});
	$("form").submit();
}

/*ajax index query all*/
function ajaxIndexQueryAll() {
	
	blockui();
	$("form").attr({'action':'index_indexQueryAll',target:'_self'});
	$("form").submit();
}

/*ajax step01*/
function ajaxStep01() {
	
	//check session user
	if (checkSessionUser() == false) {
		$('#loginModal').modal('toggle');
		return;
	}
	
	blockui();
	//redirect page
	$("form").attr({'action':'shoppingCar_step01',target:'_self'});
	$("form").submit();
}

/*ajax step0101*/
function ajaxStep0101(param) {
	
	//check session user
	if (checkSessionUser() == false) {
		$('#loginModal').modal('toggle');
		return;
	}
	
	if (!confirm('確認要取消嗎?')) {
		return;
	}
	
	blockui();
	$.ajax({
		  url: 'shoppingCar_step0101.action',
		  context: this,
		  type: 'POST',
		  dataType:"json",
		  async: false, 
		  data: {orderDetailNum : param},
		  success: function(json) {
			  if (json != null) {
				  if (json.length > 0) {
					  if (json.trim() == "true") {
						  $("form").attr({'action':'shoppingCar_step01',target:'_self'});
					      $("form").submit();
					  } else {
						  $("form").attr({'action':'home_welcome',target:'_self'});
						  $("form").submit();
					  }
				  }
			  }			  
		  },
		  error: function (xhr, ajaxOptions, thrownError) {
			  alert(xhr);
		  }
		});
}

/*ajax step02*/
function ajaxStep02() {
	
	//check session user
	if (checkSessionUser() == false) {
		return;
	}
	
	blockui();
	//redirect page
	$("form").attr({'action':'shoppingCar_step02',target:'_self'});
	$("form").submit();
}

/*cal real pay money */
function calRealPayMoney() {
	
	var totalMoney = $("input[name*='orderInformationBo.Totalmoney']").val();
	var transportPrice = $("#transportSeqid").val();
	var useShoppingMoney = $("#useShoppingMoney").val();
	
	if (totalMoney.trim() == null || totalMoney.trim() == "") {
		totalMoney = "0";
	}
	if (transportPrice.trim() == null || transportPrice.trim() == "") {
		transportPrice = "0,0";
	}
	if (useShoppingMoney.trim() == null || useShoppingMoney.trim() == "") {
		useShoppingMoney = "0";
	}
	
	totalMoney = parseInt(totalMoney,10);
	transportPrice = parseInt(transportPrice.split(',')[1],10);
	useShoppingMoney = parseInt(useShoppingMoney,10);
	
	//訂單金額 > 2000 免運費
	if (totalMoney < 2000) {
		totalMoney += transportPrice;
	}
	
	//減掉使用購物金
	if (checkStep03UseShoppingMoney()) {
		totalMoney -= useShoppingMoney;
	}
	
	$("#realPayMoney").val(totalMoney);
	$("#realPayMoneyStr").val(totalMoney);
	
}

/*check use shopping money*/
function checkStep03UseShoppingMoney() {
	
	var useShoppingMoney = $("#useShoppingMoney").val();
	var totalShoppingMoney = $("input[name*='userMemberBo.TotalShoppingMoney']").val();
	
	if (useShoppingMoney.trim() == null || useShoppingMoney.trim() == "") {
		useShoppingMoney = "0";
	}
	if (totalShoppingMoney.trim() == null || totalShoppingMoney.trim() == "") {
		totalShoppingMoney = "0";
	}
	
	useShoppingMoney = parseInt(useShoppingMoney,10);
	totalShoppingMoney = parseInt(totalShoppingMoney,10);
	
	if (useShoppingMoney > totalShoppingMoney) {
		alert("您所使用的購物金已超過所也購物金!!");
		return false;
	}
	
	return true;
}

/*ajax step03*/
function ajaxStep03() {
	
	 if (!checkStep03UseShoppingMoney()) {
		 return;
	 }
	
	//check session user
	if (checkSessionUser() == false) {
		return;
	}
	
	blockui();
	//redirect page
	$("form").attr({'action':'shoppingCar_step03',target:'_self'});
	$("form").submit();
}

/*ajax step04*/
function ajaxStep04() {
	
	//check session user
	if (checkSessionUser() == false) {
		return;
	}
	
	blockui();
	//redirect page
	$("form").attr({'action':'shoppingCar_step04',target:'_self'});
	$("form").submit();
}

/*ajax ajaxStep0401*/
function ajaxStep0401() {
	
	blockui();
	//redirect page.
	$("form").attr({'action':'home_welcome',target:'_self'});
	$("form").submit();
}

/*ajax ajaxStep0402*/
function ajaxStep0402() {
	
	blockui();
	//order query
	ajaxOrderList01();
}
